<?php

/**
 * This is the model class for table "{{adminer}}".
 *
 * The followings are the available columns in table '{{adminer}}':
 * @property integer $zda_id
 * @property string $zda_username
 * @property string $zda_password
 * @property integer $zag_id
 * @property string $zca_ids
 * @property string $zda_name
 * @property integer $zda_login
 * @property string $zda_ip
 * @property integer $zda_status
 * @property integer $inputtime
 * @property integer $updatetime
 * 张梦林
 * 2012-12-4
 */
class Adminer extends CActiveRecord
{
	//重复密码
	public $re_password;
	//旧密码
	public $old_password;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Adminer the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{adminer}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zda_username,zda_password,zpc_id,zag_id,zda_name,zda_sex,zpc_id,zag_id', 'required','on'=>'memsub, register'),
			array('old_password', 'required', 'on'=>'update_password'),
			array('zag_id,zda_sex,zpc_id', 'numerical', 'integerOnly'=>true),
			array('zda_username, zda_password, zda_duties, zda_name', 'length', 'max'=>50,'min'=>2),
			array('re_password', 'compare', 'compareAttribute'=>'zda_password','on'=>'register, reset_password, update_password','message'=>'两处输入的密码并不一致'),
			array('zda_username', 'unique','caseSensitive'=>false,'className'=>'Adminer','message'=>'用户名"{value}"已经被注册，请更换'),
			array('zca_ids', 'safe'),
			//array('zda_name, zda_ip', 'length', 'max'=>25),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zda_id, zda_username, zda_password, zag_id, zca_ids, zda_name, zda_login, zda_ip, zda_status,zda_code, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'role_log'=>array(self::HAS_MANY, 'AdmGlog', array('userid'=>'zda_id')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zda_id' => '管理员',
			'zda_username' => '用户名',
			'zda_password' => '密码',
			'old_password' => '旧密码',
			'zda_sex' => '姓别',
			'zda_duties' => '职务',
			'zpc_id' => '部门',
			'zag_id' => '管理员角色',
			'zca_ids' => '行业ID',
			'zda_name' => '管理员姓名',
			'zda_login' => '登录次数',
			'zda_ip' => '最后一次登录的IP',
			'zda_zda_code'=>'随机码',
			'zda_status' => '状态',
			'inputtime' => '创建时间',
			'updatetime' => '更新时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zda_id',$this->zda_id);
		$criteria->compare('zda_username',$this->zda_username,true);
		$criteria->compare('zda_password',$this->zda_password,true);
		$criteria->compare('zag_id',$this->zag_id);
		$criteria->compare('zca_ids',$this->zca_ids,true);
		$criteria->compare('zda_name',$this->zda_name,true);
		$criteria->compare('zda_login',$this->zda_login);
		$criteria->compare('zda_ip',$this->zda_ip,true);
		$criteria->compare('zda_status',$this->zda_status);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);

		$criteria->order = " inputtime DESC ";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	/**
	 * 保存数据前做此操作
	 * */
	protected function beforeSave()
	{
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->inputtime = time();
				$this->zda_login = '0';
				$this->zda_status = '1';
				$this->zda_code = $this->createCode();
				$this->zda_password = $this->passwordEncode($this->zda_password);
			}else{
				$this->updatetime = time();
			}
			//行业分类，多个用逗号分隔
			if(is_array($this->zca_ids)){
				$this->zca_ids = join(',',$this->zca_ids);
			}
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 保存后的操作
	 */
	protected function afterSave(){
		//添加到角色表
		$admRoleLog = AdmGlog::model()->findByAttributes(array('userid'=>$this->zda_id));
		if(empty($admRoleLog)){
			$admRoleLog = new AdmGlog();
			$admRoleLog->userid = $this->zda_id;
		}
		$admRoleLog->itemname = $this->zag_id;
		$admRoleLog->save();
		return true;
	}	
	
	/**
	 * 查找 后的操作
	 */
	protected function afterFind(){
		//行业分类，转换成数组
		$this->zca_ids = explode(',',$this->zca_ids);
		if($this->zca_ids[0]==null)
			$this->zca_ids = array();
		return true;
	}
	
	/**
	 * 状态
	 */
	public function getZdaStatus($type=null){
		if($type==null){
			return array(
				'1'=>'正常',
				'2'=>'锁定'
				);
		}else{
			$data = $this->getZdaStatus();
			if(array_key_exists($type,$data))
				return $data[$type];
		}
	}

	//加密
	public function passwordEncode( $password){
		return md5( md5( $password ).$this->zda_code);
	}
	//产生随机码
	public function createCode(){
		return create_randomstr();
	}
	//修改登录记录
	public function updateLoginInfo(){
		$model = $this->findByPk(Yii::app()->user->getId());
		$model->zda_login = $this->zda_login+1;
		$model->zda_ip = Yii::app()->request->userHostAddress;
		$model->update(array('zda_id','zda_login','zda_ip'));
	}
	
}